import time
from RemoteCommand import *
from FileOps import *

# To change this template, choose Tools | Templates
# and open the template in the editor.

__author__="rbuck"
__date__ ="$Mar 11, 2010 2:37:38 PM$"


class IperfClientCommand(Command):
    def __init__(self,host,username,commands):
        Command.__init__(self,host=host,username=username,commands=commands)

class IperfServerCommand(Command):
    def __init__(self,host,username,commands):
        Command.__init__(self,host=host,username=username,commands=commands)

    def doCommand(self):
        """ Run the command.  Signal that
        this host is done and wait for a signal to continue. """
        # run command
        assert isinstance(self.commands, str)
        
        print "[%s] Running %s" % (self.host,self.commands)
        self.child.sendline('sudo %s' % self.commands)

        self.done.set()
        self.go.wait()
        self.child.sendcontrol('c')
        self.child.prompt(timeout=None)
        self.child.close()

class Iperf:
    def __init__(self,username,server,client,bandwidth,time,outfile):
        # username to log into machine with
        self.username = username

        # ip address of server
        self.server = server

        # ip address of client
        self.client = client

        # bandwidth to run at
        self.bandwidth = bandwidth

        # time in seconds to run iperf
        self.time = time

        # temp file to store stuff
        self.outfile = outfile

        # server running iperf
        self.iperfServer = False

        # client running iperf
        self.iperfClient = False

    def startServer(self):
        print "Starting Server"
        command = 'iperf -s -u -p 4000'
        self.iperfServer = IperfServerCommand(self.server,self.username,command)
        self.iperfServer.start()
        time.sleep(2)
        
    def startClient(self):
        print "Starting Client"
        client = '5.0.0.%s' % self.server[4:]
        command = 'iperf -u -fk -c %s -b %s -p 4000 -t %s > %s' %(client,self.bandwidth,self.time,self.outfile)
        self.iperfClient = IperfClientCommand(self.client,self.username,command)
        self.iperfClient.start()

    def stop(self):
        self.iperfClient.done.wait()
        self.iperfServer.done.wait()

        self.iperfClient.go.set()
        self.iperfServer.go.set()

        self.iperfClient.join()
        self.iperfServer.join()



if __name__ == "__main__":
    #bandwidth is in kilobits
    gap = 100
    bandwidth = gap * 25

    # 5 Mbps is max right now
    max = 1000 * 5
    
    while bandwidth <= max:
        for i in range(0,1):
            bandwidth_str = '%dK' %(bandwidth)
            username = 'rbuck'
            server = 'mesh1'
            client = 'mesh13'
            duration = '10'
            hops = '4'

#            if i % 2 == 0:
#                client = 'mesh3'
#                hops = '5'

            filename = '/tmp/client-%s-server-%s-bandwidth-%s-duration-%s-hops-%s-itr-%d.txt' %(client,server,bandwidth_str,duration,hops,i)
            iperf = Iperf(username,server,client,bandwidth_str,duration,filename)
            iperf.startServer()
            iperf.startClient()
            iperf.stop()

            fg = FileGrabber(client,filename,'/home/rbuck/Desktop/iperfResults/',username)
            fg.grab()

            #node, path, username
            fr = FileRemover(client,filename,username)
            fr.remove()

        bandwidth = bandwidth + gap




